<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="Shortcut Icon" href="../images/aecc.ico" type="image/x-icon" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="../StaticFiles/element-plus-2.9.1/index.css" rel="stylesheet">
  <link href="../StaticFiles/nprogress/nprogress.min.css" rel="stylesheet">
  <link href="../styles/vue-page-common.css" rel="stylesheet">
  <script src="../Scripts/pollyfills/Array.prototype.flat.js"></script>
  <script src="../Scripts/pollyfills/ResizeObserver.js"></script>
  <script src="../StaticFiles/vue3/vue.global.prod.js"></script>
  <script src="../StaticFiles/element-plus-2.9.1/index.full.min.js"></script>
  <script src="../StaticFiles/element-plus-2.9.1/icons-vue.js"></script>
  <script src="../StaticFiles/element-plus-2.9.1/zh-cn.min.js"></script>
  <script src="../Scripts/axios.min.js"></script>
  <script src="../Scripts/axios.config.js"></script>
  <script src="../StaticFiles/nprogress/nprogress.min.js"></script>
  <script src="../Scripts/dayjs.min.js"></script>
  <script src="../Scripts/crypto-js.js"></script>
  <script src="../Scripts/secret.js"></script>
  <title>预排工序生产物资需求计划报表</title>
  <style>
    .el-table--large .cell {
      padding: 0 10px !important;
    }
  </style>
</head>

<body>
  <div id="app" v-cloak>
    <vue3-report-header title="预排工序生产物资需求计划报表"></vue3-report-header>
    <div style="padding: 10px;" @keyup.enter="getData">
      <!-- 查询条件区 -->
      <el-form :inline="true" :model="dataObj.queryInfo" ref="queryInfoRef" :label-position="'left'"
        :rules="dataObj.formRules">
        <el-form-item label="主制分厂:">
          <el-select style="width: 160px;" v-model="dataObj.queryInfo.factoryid" placeholder="请选择主制分厂" clearable
            :disabled="dataObj.factoryDisabled" filterable>
            <el-option v-for="f in dataObj.factoryList" :key="f.factoryid" :label="f.factoryname" :value="f.factoryid">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="订单号:">
          <el-autocomplete v-model.trim="dataObj.queryInfo.mfgordername" placeholder="请输入订单号" :trigger-on-focus="false"
            :fetch-suggestions="searchMfgorderAsync" :value-key="'mfgordername'" :debounce="500">
          </el-autocomplete>
        </el-form-item>

        <el-form-item label="零件图号:">
          <el-autocomplete v-model.trim="dataObj.queryInfo.productname" placeholder="请输入零件图号" :trigger-on-focus="false"
            style="width:240px;" :fetch-suggestions="searchProductAsync" :value-key="'productname'" :debounce="500">
          </el-autocomplete>
        </el-form-item>

        <el-form-item label="批次号:">
          <el-autocomplete v-model.trim="dataObj.queryInfo.containername" placeholder="请输入批次号" :trigger-on-focus="false"
            style="width:280px;" :fetch-suggestions="searchContainerAsync" :value-key="'containername'" :debounce="500">
          </el-autocomplete>
        </el-form-item>

        <el-form-item label="工序名称:">
          <el-input v-model.trim="dataObj.queryInfo.workflowstepname" placeholder="请输入工序名称" clearable></el-input>
        </el-form-item>

        <el-form-item label="类型:">
          <el-select style="width: 160px;" v-model="dataObj.queryInfo.typename" placeholder="请选择类型" clearable
            filterable>
            <el-option v-for="f in dataObj.typeList" :key="f.itemname" :label="f.itemname" :value="f.itemname">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="需求日期:" prop="starttime">
          <el-date-picker v-model="dataObj.queryInfo.starttime" type="daterange" range-separator="至"
            start-placeholder="开始日期" end-placeholder="结束日期" value-format="YYYY-MM-DD" :shortcuts="dataObj.shortcuts">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="是否齐套:">
          <el-select style="width: 160px;" v-model="dataObj.queryInfo.isallset" placeholder="请选择" clearable filterable>
            <el-option :key="'是'" :label="'是'" :value="'是'"></el-option>
            <el-option :key="'否'" :label="'否'" :value="'否'"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" :icon="'Search'" size="default" @click="getData">查询</el-button>
          <el-button type="primary" :icon="'Download'" size="default" @click="getExportExcel">导出</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格 -->
      <el-table :data="dataObj.dataList" stripe border show-overflow-tooltip v-loading.fullscreen.lock="dataObj.loading"
        :element-loading-text="dataObj.loadingText" element-loading-background="rgba(0,0,0,0.8)">
        <el-table-column type="index" width="60"></el-table-column>
        <el-table-column prop="mfgordername" label="订单号" width="120" show-overflow-tooltip></el-table-column>
        <el-table-column prop="productname" label="零件号" width="120" show-overflow-tooltip></el-table-column>
        <el-table-column prop="productdescription" label="零件名称" width="120" show-overflow-tooltip></el-table-column>
        <el-table-column prop="containername" label="批次号" width="180" show-overflow-tooltip></el-table-column>
        <el-table-column prop="workflowinfo" label="工艺规程" width="120" show-overflow-tooltip></el-table-column>
        <el-table-column prop="factoryname" label="主制分厂" width="100" show-overflow-tooltip></el-table-column>
        <el-table-column prop="specno" label="工序号" width="80"></el-table-column>
        <el-table-column prop="workflowstepname" label="工序名称" width="120" show-overflow-tooltip></el-table-column>
        <el-table-column prop="tooltype" label="类型" width="80"></el-table-column>
        <el-table-column prop="toolname" label="编码" width="160" show-overflow-tooltip></el-table-column>
        <el-table-column prop="description" label="名称" width="160" show-overflow-tooltip></el-table-column>
        <el-table-column prop="needqty" label="需求数量" width="100" show-overflow-tooltip></el-table-column>
        <el-table-column prop="expirationdate" label="有效期" width="170" :formatter="replaceDate" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="starttime" label="需求日期" width="100" :formatter="splitDate" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="iscomp" label="是否齐套" width="80" show-overflow-tooltip></el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="dataObj.currentPage" :page-sizes="[dataObj.pageSize,20,50]" :page-size="dataObj.pageSize"
        layout="prev,pager,next,jumper,sizes,total" :total="dataObj.total">
      </el-pagination>

      <!-- 回到顶部 -->
      <el-backtop></el-backtop>
    </div>
  </div>
  <script type="module">
    import Vue3ReportHeader from '../Scripts/components/Vue3ReportHeader.js';
    import { exportExcel, getFactory, fetchSuggestion, getParams } from '../Scripts/hooks/useCommonMethod.js'
    import { splitDate, replaceDate } from '../Scripts/hooks/format.js'
    import { getUserInfo } from '../Scripts/hooks/fetchAPI.js'
    import { prevWeek, thisWeek, nextWeek, prevMonth, thisMonth, nextMonth } from '../Scripts/hooks/vue3DateShortCut.js'
    const { createApp, reactive, ref, onMounted, watch } = Vue
    const { ElMessage } = ElementPlus
    const App = {
      setup() {
        const queryInfoRef = ref(null);
        const dataObj = reactive({
          loading: false,
          loadingText: '数据加载中...',
          currentPage: 1,// 当前页
          pageSize: 12, // 每页显示条数
          total: 0,
          queryInfo: {
            factoryid: '',
            productname: '',
            containername: '',
            mfgordername: '',
            workflowstepname: '',
            typename: '',
            starttime: '',
            isallset: '',
          },
          factoryDisabled: false,
          dataList: [],
          factoryList: [],
          typeList: [{ itemname: '工装工具' }, { itemname: '辅材' }, { itemname: '特殊过程' }, { itemname: '数控程序' }],
          formRules: {
            starttime: [
              { required: true, message: '【需求日期】不能为空', trigger: 'change' },
            ]
          },
          shortcuts: [prevWeek, thisWeek, nextWeek, prevMonth, thisMonth, nextMonth]
        })

        const api = '../handler/report/ApsPlanInfoPreAllSetDetailReport.ashx'

        function searchContainerAsync(value, callback) {
          fetchSuggestion({ actionName: 'getContainernameAsync', name: 'containername', value, callback })
        }

        function searchProductAsync(value, callback) {
          fetchSuggestion({ actionName: 'getProductnameAsync', name: 'productname', value, callback });
        }

        function searchMfgorderAsync(value, callback) {
          fetchSuggestion({ actionName: 'getMfgordernameAsync', name: 'mfgordername', value, callback });
        }

        // 查询数据
        async function getData() {
          const valid = await queryInfoRef.value.validate(valid => valid);
          if (!valid) return;

          dataObj.loading = true;
          dataObj.loadingText = '数据加载中...';
          const { data: { data, meta, total } } = await axios({
            url: `${api}?actions=getData`,
            method: 'post',
            data: {
              ...dataObj.queryInfo,
              currentPage: dataObj.currentPage,
              pageSize: dataObj.pageSize,
              type: 'getData'
            }
          })
          dataObj.loading = false
          if (meta.status === 500) {
            return ElMessage.error(meta.msg);
          }
          dataObj.dataList = data;
          dataObj.total = total;
        };

        // 导出excel
        function getExportExcel() {
          const config = {
            url: `${api}?actions=exportExcel`,
            data: { ...dataObj.queryInfo, type: 'exportExcel' }
          }
          exportExcel(config, dataObj, ElMessage)
        };

        function handleCurrentChange(newPage) {
          dataObj.currentPage = newPage;
          getData();
        };

        function handleSizeChange(newSize) {
          dataObj.pageSize = newSize;
          getData();
        };

        onMounted(async () => {
          const start = dayjs().startOf('week').add(1, 'day').subtract(7, 'day').format('YYYY-MM-DD');
          const end = dayjs().endOf('week').add(1, 'day').subtract(7, 'day').format('YYYY-MM-DD');
          dataObj.queryInfo.starttime = [start, end];

          const [factoryinfo, userInfo] = await Promise.all([
            getFactory(dataObj),
            getUserInfo({ employeeid: getParams("uid") })
          ]);

          if (userInfo && userInfo.factorycode) {
            const factory = dataObj.factoryList.filter(item => item.factorycode === userInfo.factorycode)
            dataObj.factoryList = factory;
            dataObj.queryInfo.factoryid = factory[0].factoryid;
            dataObj.factoryDisabled = true;
          }
        })

        watch(() => dataObj.queryInfo, () => {
          dataObj.currentPage = 1;
        }, { deep: true })

        return {
          dataObj, splitDate, replaceDate,
          getData,
          getExportExcel,
          handleCurrentChange,
          handleSizeChange,
          searchContainerAsync,
          searchProductAsync,
          searchMfgorderAsync,
          queryInfoRef
        }
      }
    };

    const app = createApp(App);
    app.use(ElementPlus, {
      locale: ElementPlusLocaleZhCn,
      size: 'large'
    });
    // 注册所有icon
    for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
      app.component(key, component)
    }
    app.component('vue3-report-header', Vue3ReportHeader);
    app.mount('#app');
  </script>
</body>

</html>